/**
 *  鉴权token
 */
const User = require('../models/User')
const jwt = require('jsonwebtoken')
module.exports = (async (req, res, next) => {
    // 获取当前访问的api地址
    const url = req.originalUrl
    console.log(url)
    // 不需要进行验证的api
    const urlArr = ['/api/login', '/api/register']
    // 验证当前的api是否存在不需要验证的api的列表里面
    const is_next = urlArr.find(item => item === url)
    if (is_next) {
        next();
        return false
    }
    // 获取api传递过来的token
    const { authorization } = req.headers
    const token = String(req.headers.authorization).split(' ').pop()
    if (!token)
        return res.send({ msg: '没有token' })
    const { userName } = jwt.verify(token, 'screen')
    console.log(userName);
    const model = await User.findOne({ where: { userName } })
    if (!model)
        return res.send({ msg: '请注册' })
    next();

})